﻿package net.wangit.util;

import net.wangit.MapList;
import net.wangit.Row;

public class TreeUtil {

	public TreeUtil() {
	}

	public static MapList getParents(MapList list, String id, String idName,
			String parentName) {
		if (Checker.isEmpty(list)) {
			return null;
		} else {
			MapList parents = new MapList();
			setParents(list, parents, id, idName, parentName);
			return parents;
		}
	}

	private static void setParents(MapList list, MapList parents, String id,
			String idName, String parentName) {
		Row row = list.findRow(idName, id);
		if (row != null) {
			String parentId = row.get(parentName);
			Row parentRow = list.findRow(idName, parentId);
			if (parentRow != null) {
				parents.insertRow(0, parentRow.toMap());
				setParents(list, parents, parentId, idName, parentName);
			}
		}
	}
}
